<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" id="form_view_id">
</div>
</div>
    <script id="form_tpl_id" type="text/html">
        <input type="hidden" name="id" value="{{d.id}}" />

    <div class="layui-form-item">
        <label class="layui-form-label required">角色</label>
        <div class="layui-input-block">
            <input type="text" name="role" lay-verify="required" lay-reqtext="角色不能为空" placeholder="请输入角色名称，注意大小写" value="{{=d.role}}" class="layui-input">
            <tip>填写角色。</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label required">类名</label>
        <div class="layui-input-block">
            <input type="text" name="class_name" lay-verify="required" lay-reqtext="类名不能为空" placeholder="请输入类名名称，注意大小写" value="{{=d.class_name}}" class="layui-input">
            <tip>填写类名。</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label required">方法名</label>
        <div class="layui-input-block">
            <input type="text" name="action_name" lay-verify="required" lay-reqtext="方法名不能为空" placeholder="请输入方法名名称，注意大小写" value="{{=d.action_name}}" class="layui-input">
            <tip>填写方法名。</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">保存权限策略</button>
        </div>
    </div>
    </script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['laytpl', 'form'], function () {
        var form = layui.form,
            layer = layui.layer,
            router = layui.router(),
            laytpl = layui.laytpl,
            $ = layui.$;

        var id = sessionStorage.getItem('edit_id'); // router.search.id
        $.getJSON('/?s=Portal.Rights.GetData&id=' + id, function (data) {
            if (data.ret != 200 || !data.data.data) {
                layer.msg(data.msg || '数据不存在或已经删除');
            }

            var getTpl = form_tpl_id.innerHTML,
                view = document.getElementById('form_view_id');

            laytpl(getTpl).render(data.data.data, function(html){
                view.innerHTML = html;
              	form.render('radio');
            });
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: '/?s=Portal.Rights.UpdateData',
                type:'post',
                data: {data: JSON.stringify(data.field), id: data.field.id},
                success:function(data){
                    if (data.ret != 200) {
                        layer.msg(data.msg);
                        return;
                    }

                    var index = layer.alert('更新成功，请刷新', function () {
                        // 关闭弹出层
                        layer.close(index);

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);

                        //window.location.reload();
                    });
                },
                complete: function () {
                }
            });

            return false;
        });

    });
</script>
</body>
</html>
